(function () {
    'use strict';

    angular
        .module('app.processreferral')
        .controller('processreferralController', processreferralController);

    processreferralController.$inject = ['logger', '$state', '$rootScope', '$stateParams', 'DefaultListsProvider','PatientDemoData','$mdDialog','$timeout'];
    /* @ngInject */
    function processreferralController(logger, $state, $rootScope, $stateParams, DefaultListsProvider, PatientDemoData, $mdDialog, $timeout) {
        var vm = this;
        vm.title = 'processreferral';
        vm.password = "";
        vm.ReferralID = $stateParams.ReferralID;
        vm.ReadOnly = true;
        vm.EditClick = EditClick;
        vm.SaveData = SaveData;
        vm.ShowControls = true;
        vm.FilterPractice = FilterPractice;

        activate();
        
        function activate() {
            vm.PracticesList = DefaultListsProvider.GetPracticesList();
            vm.Physicians = DefaultListsProvider.GetPhysiciansList();

            if ($rootScope.ReferralCategory == 'Completed' || $rootScope.ReferralCategory == 'Referral-Out') {
                vm.DisableProcess = true;
            }

            

            var promise = PatientDemoData.GetReferralProcessDetail(vm.ReferralID).then(function (response) {
                if (response.data.ErrorMessage == 'Success') {
                    vm.RefPractice = response.data.Practice;
                    vm.PreferredPhysician = response.data.PreferredPhysician;
                    vm.RefPhysician = response.data.Physician;
                    vm.ReferralAccepted = response.data.AcceptanceStatus.toString();
                    vm.ReferralNotes = response.data.AcceptanceNotes;
                    vm.PatientContact = response.data.ScheduleStatus;
                    vm.PatientContactNotes = response.data.ScheduleNotes;
                    vm.PatientVisit = response.data.VisitStatus;
                    vm.PatientVisitNotes = response.data.VisitNotes;
                    vm.ReferralModifiedBy = response.data.AcceptanceUpdatedBy;
                    vm.ReferralModifiedTime = response.data.AcceptanceTime;
                    vm.PatientContactModifiedBy = response.data.ScheduleUpdatedBy;
                    vm.PatientContactModifiedTime = response.data.ScheduleTime;
                    vm.PatientVisitModifiedBy = response.data.VisitUpdatedBy;
                    vm.PatientVisitModifiedTime = response.data.VisitTime;
                    vm.CreatedBy = response.data.CreatedBy;
                    vm.CreatedAt = response.data.CreatedDate;
                    if (response.data.AppointmentDate == '') {
                        vm.ApptDate = '';
                    }
                    else {
                        vm.ApptDate = new Date(response.data.AppointmentDate);
                    }
                    vm.ApptTime = response.data.AppointmentTime;
                    vm.NoteStatus = response.data.NoteStatus;
                    vm.NoteUpdatedBy = response.data.NoteUpdatedBy;
                    vm.NoteUpdatedTime = response.data.NoteUpdatedTime;
                    if (response.data.NoteSentDate == '') {
                        vm.NoteSentDate = '';
                    }
                    else {
                        vm.NoteSentDate = new Date(response.data.NoteSentDate);
                    }
                    if(response.data.Physician.toString() == 0)
                    {
                        vm.RefPhysician = FindPhysicianByName(response.data.PreferredPhysician);
                    }
                    vm.PrefPhone = response.data.PrefPhone;
                    vm.EstablishedPatient = response.data.EstablishedPatient;
                    vm.PPGPhysician = response.data.PPGPhysician;
                        
                }
 
            });
        }

        function FilterPractice(Phy) {
            return Phy.PracticeID === vm.RefPractice;
        }

        function EditClick() {
            vm.ReadOnly = false;
        }

        function FindPhysicianByName(pname) {
            var PhyID = 0;
            for (var i = 0; i < vm.Physicians.length; i++) {
                if (vm.Physicians[i].PhysicianName.toString().toLowerCase() == pname.toLowerCase()) {
                    PhyID = vm.Physicians[i].PhysicianID;
                    break;
                }
            }
            return PhyID;
        }

        function SaveData() {
            var AptDate;
            var NoteDate;
            if (vm.ApptDate == '')
            {
                AptDate = '';
            }
            else {
                AptDate = vm.ApptDate.format("mm/dd/yy");
            }
            if (vm.NoteSentDate == '') {
                NoteDate = '';
            }
            else {
                NoteDate  = vm.NoteSentDate.format("mm/dd/yy");
            }
            var promise = PatientDemoData.SaveReferralProcessStatus(vm.ReferralID, vm.ReferralAccepted, vm.ReferralNotes, vm.PatientContact, vm.PatientContactNotes, vm.PatientVisit, vm.PatientVisitNotes, vm.RefPhysician, vm.RefPractice, AptDate, vm.ApptTime, vm.NoteStatus, NoteDate)
            .then(function (response) {
                if (response.data.SaveStatus = 'Saved') {
                    vm.ReadOnly = true;
                    $mdDialog.show(
                          $mdDialog.alert()
                            .parent(angular.element(document.body))
                            .clickOutsideToClose(true)
                            .title('Save Status')
                            .textContent('Referral updates have been saved.')
                            .ariaLabel('Save Satus Dialog')
                            .ok('Close')
                        ).then(function (response) {
                            $timeout(function () {
                                angular.element('#BackButton').triggerHandler('click');
                            }, 100);
                        });
                }
            })
        }


        function showWait() {
            $mdDialog.show({
                controller: 'patientdemoController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        };
    }
})();


